﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using SATF.BI;
using ExtAspNet;
using System.Data;
using SATF.DB;

namespace SATF
{
    public class CaseTreeUtil
    {

        public static void CreateTree(ExtAspNet.Tree caseTree, BIUser user)
        {
            //添加根节点
            IList<BIProduct> products = user.GetProducts();
            if (products != null)
            {
                foreach (BIProduct product in products)
                {
                    TreeNode node = new TreeNode();
                    node.Text = string.Format("<span class='nonestyle'>{0}</span>",product.Name);
                    node.NodeID = product.GetType().ToString() + "_" + product.ID.ToString();
                    node.AutoPostBack = true;
                    node.EnablePostBack = true;
                    node.Leaf = false;
                    node.Expanded = true;
                    caseTree.Nodes.Add(node);
                    ResolveSubTree(node, product);
                }
            }

            
            //添加user所属组相关的机器
            //ResolveSubTreeByGroupID(node, userGroupID);

        }

        private static void ResolveSubTree(ExtAspNet.TreeNode treeNode, BaseItem item)
        {
            //退出条件
            if (item.GetChildTable() == "" )
            {
                treeNode.Leaf = true;
                return;
            }
           
            //获取孩子节点，对每个item都添加孩子
            IList<BaseItem> children = item.GetChildren();
            if (children != null)
            {
                foreach (BaseItem child in children)
                {
                    TreeNode node = new TreeNode();
                    node.Text = string.Format("<span class='nonestyle'>{0}</span>", child.Name);
                    node.NodeID = child.GetType().ToString() + "_" + child.ID.ToString();
                    node.AutoPostBack = true;
                    node.EnablePostBack = true;
                    node.Expanded = true;
                    node.Leaf = false;
                    treeNode.Nodes.Add(node);
                    ResolveSubTree(node, child);
                }
            }

        }


/*
        private static void ResolveSubTreeByGroupID(ExtAspNet.TreeNode treeNode, UInt64 userGroupID)
        {
            BIUserGroup group = new BIUserGroup(userGroupID);
            //添加当前节点
            TreeNode node = new TreeNode();
            node.Text = group.Name;
            node.EnableCheckBox = true;
            node.AutoPostBack = true;
            node.NodeID = "node_group_" + userGroupID.ToString();
            node.Expanded = true;
            treeNode.Nodes.Add(node);

            //获取所有ostype集合
            DataTable table = BIMachine.GetOSTypeByGroupID(userGroupID);
            foreach (DataRow row in table.Rows)
            {
                int osType = Convert.ToInt32(row[0].ToString());
                ResolveSubTreeByOsType(node, userGroupID, osType);
            }
            
        }

        private static void ResolveSubTreeByOsType(ExtAspNet.TreeNode treeNode, UInt64 userGroupID, int osType)
        {
            //添加当前节点
            TreeNode node = new TreeNode();
            node.Text = BIConverters.OSType(osType);
            node.NodeID = "node_ostype_" + userGroupID.ToString() + "_" + osType.ToString();
            node.Expanded = true;
            node.EnableCheckBox = true;
            node.AutoPostBack = true;
            treeNode.Nodes.Add(node);

            //添加ostype下的所有node
            BIMachines machines = BIMachines.GetMachinesByOsType(userGroupID, osType);
            foreach (BIMachine machine in machines)
            {
                TreeNode leaf = new TreeNode();
                leaf.Text = machine.IP + " " + machine.Name + " " + 
                    BIConverters.IEType(machine.IE_Type) + " " +
                    BIConverters.TestMachineState(machine.State);
                leaf.NodeID = "node_leaf_" + machine.ID.ToString();
                leaf.EnableCheckBox = true;
                leaf.AutoPostBack = true;
                leaf.Expanded = true;
                
                leaf.Leaf = true;
                node.Nodes.Add(leaf);
            }
        }*/
    }
}